import React, {useEffect, useState} from "react";
import {Link, useModel} from "@@/exports";
import {getGoods, getNewGoods, getRecommend, searchByName} from "@/services/backend/generatorController";
import {Card, Descriptions, DescriptionsProps, Flex, Image, Input, List, message, Tabs, Tag, Typography} from "antd";
import {PageContainer} from "@ant-design/pro-components";
import moment from "moment";
import {getSellOrder, getUserOrder} from "@/services/backend/OrderController";

/**
 * 主页
 * @constructor
 */
const OrderPage: React.FC = () => {
  const [loading, setLoading] = useState<boolean>(true);
  const [dataList, setDataList] = useState<Admin.Order[]>([]);
  const { initialState } = useModel('@@initialState');
  const { currentUser } = initialState ?? {};
  const myId=currentUser?.id;

  /**
   * 搜索
   */
  const doSearch = async (classify:string) => {
    setLoading(true);
    try {
      if(classify==="buy"){
        const res=await getUserOrder(myId??0);
        setDataList(res)
      }else if(classify==="sell"){
        const res=await getSellOrder(myId??0);
        setDataList(res)
      }


    } catch (error: any) {
      message.error('获取数据失败，' + error.message);
    }
    setLoading(false);
  };

  useEffect(() => {
    doSearch("buy");
  }, [1]);

  const getStatus=(status:number)=>{
    console.log(status)
    switch (status){
      case 0:
        return "已下单";
      case 1:
        return "商家已发货";
      case 2:
        return "已完成";
      default:
        return "未知";
    }
  }

  return (
    <PageContainer title={<></>}>

      <Tabs
        size="large"
        defaultActiveKey="all"
        items={[
          {
            key: 'buy',
            label: '购买的订单',
          },
          {
            key: 'sell',
            label: '售卖的订单',
          },
        ]}
        onChange={(activeKey) => {
          doSearch(activeKey);
        }
        }
      />

      <List<Admin.Order>
        rowKey="id"
        loading={loading}
        dataSource={dataList}
        pagination={{
          defaultPageSize:8,
        }}
        renderItem={(data) => (
          <List.Item>
            <Link to={`/order/detail/${data.id}`}>
              <Card>
                <Descriptions>
                  <Descriptions.Item label="订单编号">{data.orderId}</Descriptions.Item>
                  <Descriptions.Item label="商品">{data.goodsName}</Descriptions.Item>
                  <Descriptions.Item label="买家">{data.userAccount}</Descriptions.Item>
                  <Descriptions.Item label="订单状态">
                    {getStatus(data.orderStatus)}
                  </Descriptions.Item>
                  <Descriptions.Item label="下单时间">
                    {data.checkoutTime===null?"":moment(data.checkoutTime).format('YYYY-MM-DD HH:mm:ss')}
                  </Descriptions.Item>
                </Descriptions>
              </Card>
            </Link>
          </List.Item>
        )}
      />
    </PageContainer>
  );
};

export default OrderPage;
